<?php

/*
 * @author Ebrain Cedeno <ebraincedeno@gmail.com>
 */
class CategoryController extends Controller{
    
    public function actionNew(){
        VeCms::AccessValidate(array("admin"=>array(0,1)));
        
        $category = new category();
        
        if (isset($_POST['guardar'])){
            $category->setAttributes($_POST);
            if($category->validate()){
                if ($category->save()){
                    TForm::addMessage("registrado", "El registro se ha realizado correctamente");
                    $this->redirec(VeCms::createUrl('./grid'),5);
                }
                else{
                    throw new Exception("Error al intentar realizar el registro");
                }
            }
        }
        
        $this->link_back = new TLink("Regresar");
            $js_link = "parent.history.back(); 
                        return false; 
                        ";
        $this->link_back->onClick = $js_link;
        $this->link_back->OptionsAditional['style'][] = "color:#A9BCF5";
        $this->link_back->OptionsAditional['style'][] = "font-weight:bold";
        
        $this->form = new TForm();
        $this->form->Method = 'POST';
        $this->form->Action = './new';
        
        $this->description_label = new TLabel("Descripcion:");
        $this->description_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        
        $this->description_text = new TTextView();
        $this->description_text->Name = "description";
        $this->description_text->Id = $this->description_text->Name;
        $this->description_text->Cols = 10;
        $this->description_text->Rows = 10;
        $this->description_text->Width = 250;
        $this->description_text->Height = 100;
        $this->description_text->Text = $category->description;
        $this->description_text->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->button_cancel = new TButton("Cancelar");
        $this->button_cancel->Type = "reset";
        $this->button_cancel->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
        $this->button_guardar = new TButton("Guardar");
        $this->button_guardar->Name = "guardar";
        $this->button_guardar->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
    }
    
    public function actionGrid(){
        
        VeCms::AccessValidate(array("admin"=>array(0,1)));
        
        if (isset($_GET['del'])){
            TForm::addMessage("eliminado", "El registro se ha eliminado correctamente");
            $this->validate = new TForm(); 
            $this->del_category = $_GET['del'];
        }
        else{
            $this->del_category=false;
        }
        
        //botton para agregar nuevos roles
        
        $this->button_new_category = new TButton("Nueva Categoria");
        $this->button_new_category->Width = 140;
        $js_rol = '$(location).attr("href","./new");';
        
        $this->button_new_category->onClick = $js_rol;
        $this->button_new_category->OptionsAditional = array('style'=>array('font-weight:bold',
                                                                        'cursor:pointer',
                                                                        'border:1px solid gray',
                                                                        'background-image:url('.VeCms::getPathImage().VeCmsEnum::ICON_ADD.')',
                                                                        'background-position:left',
                                                                        'background-repeat: no-repeat',
                                                                        'background-position-x:5px',
                                                                        'text-align: right'));
        $offset = (isset($_GET['offset']))?$_GET['offset']:0;
        $class_category = new category();
        $class_category->Limit = 10;
        $class_category->Offset = $offset;
        
        
        $this->grid_category = new TTable();
        $this->grid_category->Id="grid-category";
        $this->grid_category->Columns = 5;
        $this->grid_category->Rows = $class_category->Limit;
        $this->grid_category->Width = '100%';
        $this->grid_category->Headers = array("Nro","Descripcion","","","");
        $this->grid_category->WidthCell[0] = 25;
        $this->grid_category->WidthCell[2] = 25;
        $this->grid_category->WidthCell[3] = 25;
        $this->grid_category->WidthCell[4] = 25;
        $this->grid_category->RowsAlign[0] = 'center';
        $this->grid_category->RowsAlign[1] = 'left';
        $this->grid_category->Pagination = new TPagination(array('offset'=>$offset,"limit"=>$class_category->Limit,'total'=>  category::model()->Count("active=:active",array(":active"=>1))));
        
        $category = $class_category->findAll("active=:active",array(":active"=>1));
        $count = 1;
        foreach ($category as $key=>$value){
            
            $view = new TImage();
            $view->onClick = '$(location).attr("href","./view?id_category='.$value->id_category.'")';
            
            $view->Src = VeCms::getPathImage().  VeCmsEnum::ICON_VIEW;
            $view->OptionsAditional['style'][]='cursor:pointer';
            
            $edit = new TImage();
            $edit->onClick = '$(location).attr("href","./update?id_category='.$value->id_category.'")';
            $edit->Src = VeCms::getPathImage().  VeCmsEnum::ICON_EDIT;
            $edit->OptionsAditional['style'][]='cursor:pointer';
            
            $delete = new TImage();
            $delete->onClick = 'if (confirm(\'Esta seguro que desea eliminar esta categoria\')){
                                    $(location).attr("href","./delete?id_category='.$value->id_category.'");
                                }';
            $delete->Src = VeCms::getPathImage().  VeCmsEnum::ICON_DELETE;
            $delete->OptionsAditional['style'][]='cursor:pointer';
            
            $this->grid_category->Items[] = array($count,$value->description,$view->show(),$edit->show(),$delete->show());
            $count++;
        }
        
    }
    
    public function actionUpdate(){
        
        VeCms::AccessValidate(array("admin"=>array(0,1)));
        
        $class_category = new category();
        
        if (isset($_POST['actualizar'])){
            $class_category->setAttributes($_POST);
            if($class_category->validate()){
                if ($class_category->update()){
                    TForm::addMessage("actualizado", "El registro se ha actualizado correctamente");
                    $this->redirec(VeCms::createUrl('./grid'),5);
                }
                else{
                    throw new Exception("Error al intentar actualizar el registro");
                }
            }
        }
        
        $this->link_back = new TLink("Regresar");
            $js_link = "parent.history.back(); 
                        return false; 
                        ";
        $this->link_back->onClick = $js_link;
        $this->link_back->OptionsAditional['style'][] = "color:#A9BCF5";
        $this->link_back->OptionsAditional['style'][] = "font-weight:bold";
        
        $category = $class_category->find("id_category=:id_category",array(":id_category"=>$_GET['id_category']));
        
        $this->form = new TForm();
        $this->form->Method = 'POST';
        $this->form->Action = './update?id_category='.$_GET['id_category'];
        
        $this->description_label = new TLabel("Descripcion:");
        $this->description_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        
        $this->description_text = new TTextView();
        $this->description_text->Name = "description";
        $this->description_text->Id = $this->description_text->Name;
        $this->description_text->Cols = 10;
        $this->description_text->Rows = 10;
        $this->description_text->Width = 250;
        $this->description_text->Height = 100;
        $this->description_text->Text = $category->description;
        $this->description_text->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->id_category_hidden = new TEdit();
        $this->id_category_hidden->Name = 'id_category';
        $this->id_category_hidden->Id = $this->id_category_hidden->Name;
        $this->id_category_hidden->Text = $category->id_category; 
        $this->id_category_hidden->IsHidden = true;
        
        $this->button_cancel = new TButton("Cancelar");
        $this->button_cancel->Type = "reset";
        $this->button_cancel->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
        $this->button_guardar = new TButton("Actualizar");
        $this->button_guardar->Name = "actualizar";
        $this->button_guardar->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
        
        
    }
    
    public function actionDelete(){
        
        VeCms::AccessValidate(array("admin"=>array(0,1)));
        
        if (isset($_GET['id_category'])){
            $category = category::model()->find("id_category=:id_category",array(":id_category"=>$_GET['id_category']));
            $category->active = 0;
            if ($category->update()){
                $this->redirec(VeCms::createUrl("./grid"),0,array("del"=>true));
            }
        }
    }
    
    public function actionView(){
        
        if (isset($_GET['id_category'])){
            
            $category = category::model()->find("id_category=:id_category",array(':id_category'=>$_GET['id_category']));
            
            $this->link_back = new TLink("Regresar");
            $js_link = "parent.history.back(); 
                        return false; 
                        ";
            $this->link_back->onClick = $js_link;
            $this->link_back->OptionsAditional['style'][] = "color:#A9BCF5";
            $this->link_back->OptionsAditional['style'][] = "font-weight:bold";
            
            $this->id_category_label = new TLabel("Id Categoria:");
            $this->id_category_label->OptionsAditional['style'][] = 'font-weight:bold';
            $this->id_category_value = new TLabel($category->id_category);
            
            $this->description_label = new TLabel("Descripcion:");
            $this->description_label->OptionsAditional['style'][] = 'font-weight:bold';
            $this->description_value = new TLabel($category->description);
            
        }
        else{
            throw new Exception("Error al intentar generar la vista");
        }
        
    }
}
?>
